/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Interface;

import java.util.List;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.TableModel;
import negocio.Tabela;
import negocio.Coluna;

/**
 *
 * @author v3550
 */
public class FormTable extends javax.swing.JInternalFrame implements TableModelListener {

    /**
     * Creates new form FormTable
     */
    static FormTable instance;
    Controlador controlador;
    String owner, tabela;
    List<Coluna> list;
    private TableModel model;
    private JTable jTabela;

    public FormTable(String user, String table) throws Exception {
        super(user + " - " + table);
        this.owner   = user;
        this.tabela  =  table;
        this.controlador = Controlador.getInstance();
        
        list = controlador.getColunas(this.owner, this.tabela);
        initComponents();
    }

    private JTable getTables() {
        if (jTabela == null) {
            jTabela = new JTable();
            jTabela.setModel(getTableModel());
        }
        return jTabela;
    }

    private TableModel getTableModel() {
        if (model == null) {
            model = new ModelTable(list);
        }
        return model;
    }

    public void setConteudo(String owner, String tabela) throws Exception {

        this.owner = owner;
        this.tabela = tabela;

        Tabela d = controlador.recuperarDicionario(owner, tabela);
        
        if (d != null) {
            jTextTaxaCrescimento.setText("" + d.getTaxaCrescimentoAnual());
            jTextLinhaInicial.setText("" + d.getLinhaEstimadoIncial());
            int reterDados = (d.getReterDados() ==0) ? 5 : d.getReterDados();
            jTextReterDados.setText("" + reterDados);
            jTextInitialBruto.setText(String.format("%6.2f",(d.getInicialBruto()/1024)));
            jTextInitialLiquido.setText(String.format("%6.2f",(d.getInicialLiquido()/ 1024)));
            JtextNextLiq.setText(String.format("%6.2f",(d.getNextLiquido()/ 1024)));
            JtextNextBru.setText(String.format("%6.2f",(d.getNextBruto()/ 1024)));
            jTextVlrBruto.setText(String.format("%6.2f",(d.getValorBruto()/ 1024)));
        } else {
            jTextTaxaCrescimento.setText("");
            jTextLinhaInicial.setText("");
            jTextReterDados.setText("5");
            jTextInitialBruto.setText("");
            jTextInitialLiquido.setText("");
            JtextNextLiq.setText("");
            JtextNextBru.setText("");
            jTextVlrBruto.setText("");

        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel = new javax.swing.JPanel();
        jTextLinhaInicial = new javax.swing.JTextField();
        jTextTaxaCrescimento = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable = new javax.swing.JTable();
        jTextReterDados = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jLabel6 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jTextInitialLiquido = new javax.swing.JTextField();
        JtextNextLiq = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jButton2 = new javax.swing.JButton();
        jLabel7 = new javax.swing.JLabel();
        jTextVlrBruto = new javax.swing.JTextField();
        jTextInitialBruto = new javax.swing.JTextField();
        jLabel12 = new javax.swing.JLabel();
        JtextNextBru = new javax.swing.JTextField();
        jLabel14 = new javax.swing.JLabel();

        getContentPane().setLayout(new java.awt.GridLayout(1, 0));

        jPanel.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
        jPanel.add(jTextLinhaInicial, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 30, 65, -1));
        jPanel.add(jTextTaxaCrescimento, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 82, 36, -1));

        jTable.setModel(getTableModel());
        jTable.getTableHeader().setReorderingAllowed(false);
        jScrollPane1.setViewportView(jTable);

        jPanel.add(jScrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 115, 664, 249));

        jTextReterDados.setText("5");
        jPanel.add(jTextReterDados, new org.netbeans.lib.awtextra.AbsoluteConstraints(93, 30, 40, -1));

        jLabel1.setText("Linhas inicias:");
        jPanel.add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 10, -1, -1));

        jLabel2.setText("Taxa Crescimento:");
        jPanel.add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 61, -1, -1));

        jLabel3.setText("Retenção dos dados:");
        jPanel.add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(93, 11, -1, -1));

        jButton1.setText("Gravar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        jPanel.add(jButton1, new org.netbeans.lib.awtextra.AbsoluteConstraints(93, 81, 102, -1));

        jLabel6.setText("(Anos)");
        jPanel.add(jLabel6, new org.netbeans.lib.awtextra.AbsoluteConstraints(151, 30, -1, 20));

        jLabel9.setText("(%)");
        jPanel.add(jLabel9, new org.netbeans.lib.awtextra.AbsoluteConstraints(56, 82, -1, 20));
        jPanel.add(jTextInitialLiquido, new org.netbeans.lib.awtextra.AbsoluteConstraints(340, 30, 66, -1));
        jPanel.add(JtextNextLiq, new org.netbeans.lib.awtextra.AbsoluteConstraints(340, 80, 63, -1));

        jLabel4.setText("Ini Líquido:");
        jPanel.add(jLabel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(340, 10, -1, -1));

        jLabel5.setText("Next Líq:");
        jPanel.add(jLabel5, new org.netbeans.lib.awtextra.AbsoluteConstraints(340, 60, -1, -1));

        jButton2.setText("Cálc. Volumétrico");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        jPanel.add(jButton2, new org.netbeans.lib.awtextra.AbsoluteConstraints(490, 80, -1, -1));

        jLabel7.setText("Total:");
        jPanel.add(jLabel7, new org.netbeans.lib.awtextra.AbsoluteConstraints(490, 10, -1, -1));
        jPanel.add(jTextVlrBruto, new org.netbeans.lib.awtextra.AbsoluteConstraints(490, 30, 74, -1));
        jPanel.add(jTextInitialBruto, new org.netbeans.lib.awtextra.AbsoluteConstraints(420, 30, 56, -1));

        jLabel12.setText("Ini Bruto:");
        jPanel.add(jLabel12, new org.netbeans.lib.awtextra.AbsoluteConstraints(420, 10, -1, -1));
        jPanel.add(JtextNextBru, new org.netbeans.lib.awtextra.AbsoluteConstraints(420, 80, 56, -1));

        jLabel14.setText("Next Bruto:");
        jPanel.add(jLabel14, new org.netbeans.lib.awtextra.AbsoluteConstraints(420, 60, -1, -1));

        getContentPane().add(jPanel);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:
        try{
            controlador.calculoVolumetrico(this.owner);
            setConteudo(this.owner, this.tabela) ;

        }catch(Exception ex)
        {
            JOptionPane.showMessageDialog(this, "cálculo volumétrico \n" + ex.getMessage());
        }
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        //primeiraGravacao();
        segundaGravacao();
    }//GEN-LAST:event_jButton1ActionPerformed
    private void segundaGravacao() {
        try {
            Integer linhaInicial = Integer.parseInt(jTextLinhaInicial.getText().trim());
            float taxaCrescimento = Float.parseFloat(jTextTaxaCrescimento.getText().trim());
            Integer reterDados = Integer.parseInt(jTextReterDados.getText().trim());
            boolean var = controlador.gravarNoDicionario(this.tabela, this.owner, linhaInicial, taxaCrescimento, reterDados, list);
            if (var) {
                JOptionPane.showMessageDialog(this, "Gravado com sucesso");
            }
        } catch (NumberFormatException e) {
            JOptionPane.showMessageDialog(this, "Algum valor não foi preenchido \n:" + e.getMessage());
        } catch (Exception x) {
            JOptionPane.showMessageDialog(this, "Atenção: \n:" + x.getMessage());
        }
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField JtextNextBru;
    private javax.swing.JTextField JtextNextLiq;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable;
    private javax.swing.JTextField jTextInitialBruto;
    private javax.swing.JTextField jTextInitialLiquido;
    private javax.swing.JTextField jTextLinhaInicial;
    private javax.swing.JTextField jTextReterDados;
    private javax.swing.JTextField jTextTaxaCrescimento;
    private javax.swing.JTextField jTextVlrBruto;
    // End of variables declaration//GEN-END:variables

    @Override
    public void tableChanged(TableModelEvent e) {
        int row = e.getFirstRow();
        int column = e.getColumn();
        TableModel model = (TableModel) e.getSource();
        String columnName = model.getColumnName(column);
        Object data = model.getValueAt(row, column);
        JOptionPane.showMessageDialog(this, "Atenção: \n:" + data.toString());
    }
}
